<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Exercise 4.60: Log-optimal investment strategy</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch04_cvx_opt_probs/html/logopt_investment.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Exercise 4.60: Log-optimal investment strategy</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Jo&Atilde;&laquo;lle Skaf - 04/24/08</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% The investment strategy x that maximizes the long term growth rate</span>
<span class="comment">%           R = sum_{j=1}^m pi_j*log(p_j^Tx)</span>
<span class="comment">% is called the log-optimal investment strategy, and can be found by</span>
<span class="comment">% solving the optimization problem</span>
<span class="comment">%           maximize    sum_{j=1}^m pi_j log(p_j^Tx)</span>
<span class="comment">%           subject to  x &gt;= 0, sum(x) = 1,</span>
<span class="comment">% where p_ij is the return of asset i over one period in scenario j and</span>
<span class="comment">% pi_j is the probability of scenario j. There are n assets and m scenarios.</span>
<span class="comment">% We consider the case of equiprobable scenarios.</span>
<span class="comment">%</span>
<span class="comment">% The log-optimal long term growth rate is found and compared to the one</span>
<span class="comment">% obtained with a uniform allocation strategy, i.e., x_i=(1/n).</span>
<span class="comment">% Sample trajectories ofthe accumulated wealth for the optimal strategy and</span>
<span class="comment">% the uniform one are plotted.</span>


<span class="comment">% Input data</span>
P = [3.5000    1.1100    1.1100    1.0400    1.0100;
     0.5000    0.9700    0.9800    1.0500    1.0100;
     0.5000    0.9900    0.9900    0.9900    1.0100;
     0.5000    1.0500    1.0600    0.9900    1.0100;
     0.5000    1.1600    0.9900    1.0700    1.0100;
     0.5000    0.9900    0.9900    1.0600    1.0100;
     0.5000    0.9200    1.0800    0.9900    1.0100;
     0.5000    1.1300    1.1000    0.9900    1.0100;
     0.5000    0.9300    0.9500    1.0400    1.0100;
     3.5000    0.9900    0.9700    0.9800    1.0100];

[m,n] = size(P);
Pi = ones(m,1)/m;
x_unif = ones(n,1)/n; <span class="comment">% uniform resource allocation</span>

<span class="comment">% Find the log-optimal investment policy</span>
cvx_begin
    variable <span class="string">x_opt(n)</span>
    maximize <span class="string">sum(Pi.*log(P*x_opt))</span>
    sum(x_opt) == 1
    x_opt &gt;= 0
cvx_end

<span class="comment">% Long-term growth rates</span>
R_opt = sum(Pi.*log(P*x_opt));
R_unif = sum(Pi.*log(P*x_unif));
display(<span class="string">'The long term growth rate of the log-optimal strategy is: '</span>);
disp(R_opt);
display(<span class="string">'The long term growth rate of the uniform strategy is: '</span>);
disp(R_unif);

<span class="comment">% Generate random event sequences</span>
rand(<span class="string">'state'</span>,10);
N = 10;  <span class="comment">% number of random trajectories</span>
T = 200; <span class="comment">% time horizon</span>
w_opt = []; w_unif = [];
<span class="keyword">for</span> i = 1:N
    events = ceil(rand(1,T)*m);
    P_event = P(events,:);
    w_opt = [w_opt [1; cumprod(P_event*x_opt)]];
    w_unif = [w_unif [1; cumprod(P_event*x_unif)]];
<span class="keyword">end</span>

<span class="comment">% Plot wealth versus time</span>
figure
semilogy(w_opt,<span class="string">'g'</span>)
hold <span class="string">on</span>
semilogy(w_unif,<span class="string">'r--'</span>)
grid
axis <span class="string">tight</span>
xlabel(<span class="string">'time'</span>)
ylabel(<span class="string">'wealth'</span>)
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling Mosek 9.1.9: 36 variables, 15 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 15              
  Cones                  : 10              
  Scalar variables       : 36              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 1
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 15              
  Cones                  : 10              
  Scalar variables       : 36              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 4
Optimizer  - Cones                  : 10
Optimizer  - Scalar variables       : 35                conic                  : 30              
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 10                after factor           : 10              
Factor     - dense dim.             : 0                 flops                  : 4.58e+02        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.0e+00  1.1e+00  1.5e+01  0.00e+00   1.350234740e+01   -8.051020016e-01  1.0e+00  0.00  
1   1.9e-01  2.1e-01  9.3e-01  6.72e-01   1.712662212e+00   -1.181631184e+00  1.9e-01  0.01  
2   4.7e-02  5.2e-02  1.1e-01  2.36e+00   2.856263594e-01   -9.752726878e-02  4.7e-02  0.01  
3   6.3e-03  7.0e-03  5.2e-03  1.18e+00   5.192195689e-02   4.471645465e-03   6.3e-03  0.01  
4   2.6e-04  2.9e-04  4.2e-05  1.03e+00   2.411322106e-02   2.216544110e-02   2.6e-04  0.01  
5   2.1e-05  2.3e-05  9.4e-07  1.00e+00   2.315827542e-02   2.300255456e-02   2.1e-05  0.01  
6   1.5e-06  1.6e-06  1.8e-08  1.00e+00   2.308374137e-02   2.307274665e-02   1.5e-06  0.01  
7   1.6e-07  1.8e-07  6.3e-10  1.00e+00   2.307890232e-02   2.307770840e-02   1.6e-07  0.01  
8   1.7e-08  1.9e-08  2.2e-11  1.00e+00   2.307838501e-02   2.307825640e-02   1.7e-08  0.01  
9   6.8e-09  7.6e-09  5.1e-12  1.13e+00   2.307834382e-02   2.307829450e-02   7.1e-09  0.01  
10  4.7e-09  3.2e-09  1.3e-12  1.11e+00   2.307832859e-02   2.307830820e-02   3.3e-09  0.01  
11  6.9e-09  1.7e-09  5.0e-13  9.89e-01   2.307832350e-02   2.307831275e-02   1.9e-09  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 2.3078323495e-02    nrm: 1e+00    Viol.  con: 2e-09    var: 4e-10    cones: 0e+00  
  Dual.    obj: 2.3078312755e-02    nrm: 1e+00    Viol.  con: 0e+00    var: 7e-10    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 11        time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.0230783
 
The long term growth rate of the log-optimal strategy is: 
    0.0231

The long term growth rate of the uniform strategy is: 
    0.0114

</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="logopt_investment__01.png" alt=""> 
</div>
</div>
</body>
</html>